window.addEvent("domready",function(){
	// Fx.Opacity definition
	Fx.Opacity=Fx.Style.extend({initialize: function(el, options){this.now = 1;this.parent(el,'opacity',options);},toggle:function(){return this.start((this.now > 0)?(1,0):(0,1));},show:function(){return this.set(1);}});
	// Fx.Scroll definition
	Fx.Scroll = Fx.Base.extend({
	options: {overflown: [],offset: {'x': 0, 'y': 0},wheelStops: true},
	initialize: function(element, options){
	this.now = [];this.element = $(element);this.bound = {'stop': this.stop.bind(this, false)};this.parent(options);
	if (this.options.wheelStops){this.addEvent('onStart', function(){document.addEvent('mousewheel', this.bound.stop);}.bind(this));
	this.addEvent('onComplete', function(){document.removeEvent('mousewheel', this.bound.stop);
	}.bind(this));}},
	setNow: function(){for (var i = 0; i < 2; i++) this.now[i] = this.compute(this.from[i], this.to[i]);},
	scrollTo: function(x, y){
	if (this.timer && this.options.wait) return this;
	var el = this.element.getSize();
	var values = {'x': x, 'y': y};
	for (var z in el.size){
	var max = el.scrollSize[z] - el.size[z];
	if ($chk(values[z])) values[z] = ($type(values[z]) == 'number') ? values[z].limit(0, max) : max;
	else values[z] = el.scroll[z];
	values[z] += this.options.offset[z];}return this.start([el.scroll.x, el.scroll.y], [values.x, values.y]);},
	toTop: function(){return this.scrollTo(false, 0);},
	toBottom: function(){return this.scrollTo(false, 'full');},
	toLeft: function(){return this.scrollTo(0, false);},
	toRight: function(){return this.scrollTo('full', false);},
	toElement: function(el){var parent = this.element.getPosition(this.options.overflown);var target = $(el).getPosition(this.options.overflown);return this.scrollTo(target.x - parent.x, target.y - parent.y);},
	increase: function(){this.element.scrollTo(this.now[0], this.now[1]);}});
	
	// main loop
	$$('.gk_vm_lp_wrapper1').each(function(el,i){
		
		// base variables
		var actual_page = 0;
		var table = el;
		var TID = el.getProperty("id");
		// generating $Gavick object
		var $G = $Gavick['gk_vm_lp-'+TID]; 
		// creating tables
		var nav = $ES('.gk_vm_lp_navigate')[i];
		var items = $ES('.gk_vm_lp_table_item',table);
		var tds = $ES('td',table);
		var max_height = 0;
		
		items.each(function(elm,j){
			elm.setStyle("width", table.getSize().size.x+"px");
		});
		
		tds.each(function(elmt,k){
			if(elmt.getSize().size.y > max_height) max_height = elmt.getSize().size.y;	
		});
		
		tds.each(function(elmt,k){
			elmt.setStyle("height", max_height+"px");	
		});
		
		// preparing animation
		var animationPeriod = false; // flag for animation time
		var animationTime = $G['anim_speed'];

		// operating on buttons
		if(tds.length > $G['amount']){
			var prev = $ES('span', nav)[0];
			var next = $ES('span', nav)[1];
			var next_active = true;
			var prev_active = false;
			prev.setOpacity(0.45);
			
			var scroller = new Fx.Scroll(table,{duration: animationTime, transition:Fx.Transitions.Quint.easeOut});
			
			next.addEvent("click",function(){
				if(next_active){
					if(!animationPeriod){
						animationPeriod = true;
					
						if(actual_page == 0){
							prev.setOpacity(1);
							prev_active = true;
						}
						
						actual_page++;
						if(((actual_page+1) * $G['amount']) >= tds.length){
							next.setOpacity(0.45);
							next_active = false;
						}
					
						scroller.toElement(items[actual_page]);
					
						(function(){animationPeriod = false;}).delay(animationTime);
					}
				}
			});
			
			prev.addEvent("click",function(){
				if(prev_active){
					if(!animationPeriod){
						animationPeriod = true;
					
						if(((actual_page) * $G['amount']) < tds.length){
							next.setOpacity(1);
							next_active = true;
						}
					
						actual_page--;
				
						if(actual_page == 0){
							prev.setOpacity(0.45);
							prev_active = false;
						}
	
						scroller.toElement(items[actual_page]);
					
						(function(){animationPeriod = false;}).delay(animationTime);
					}
				}
			});
		}
	});
});